//
//  UIBarButtonItem.swift
//  WeiBoSwift
//
//  Created by Jacky Sun on 16/4/20.
//  Copyright © 2016年 syf. All rights reserved.
//

import UIKit

extension UIBarButtonItem {
    
    class func createIMgBarbuttonItem(imgStr: String, target: AnyObject? ,action: Selector) ->UIBarButtonItem {
       let btn = UIButton()
        btn.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
        btn .setImage(UIImage(named: imgStr), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: imgStr + "_highlighted"), forState: UIControlState.Highlighted)
        btn.addTarget(target, action: action, forControlEvents: UIControlEvents.TouchUpInside)
        return UIBarButtonItem(customView: btn)
        
    }
    
    
    convenience init(imgStr: String, target: AnyObject? ,action: String?) {
        
        let btn = UIButton(type: .Custom)
        btn.setImage(UIImage(named:imgStr), forState: .Normal)
        btn.setImage(UIImage(named:imgStr + "_highlighted"), forState: .Highlighted)
        
        if let realAction = action{
            btn.addTarget(target, action: Selector(realAction), forControlEvents: .TouchUpInside)
        }
        btn.sizeToFit()
        self.init(customView: btn)
    }
    
}
